(function(){

YAHOO.pendular.Repository = function(config){
    if (!config){
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        url: "SolutionRepositoryService"
    });
    this.url = config.url;
};

YAHOO.lang.extend(YAHOO.pendular.Repository, Object, {
    entries: {},
    createFolder: function(config){
        if (!config) {
            config = {};
        }
        YAHOO.lang.augmentObject(config, {
            solution: "",
            path: "",
            name: ""
        });
        var me = this,
            url = this.url +
                "?component=createNewFolder" +
                "&solution=" + config.solution +
                "&path=" + config.path +
                "&name=" + config.name
        ;
        YAHOO.util.Connect.asyncRequest("POST", url, {
            success: function(o){
                if (o.responseXML.documentElement.tagName==="result" &&
                    o.responseXML.documentElement.childNodes[0].data==="true"
                ) {
                    if (callback.success) {
                        callback.success.call(
                            callback.scope ? callback.scope : window, 
                            me,
                            o
                        );
                    }
                }
                else {
                    if (callback.failure) {
                        callback.failure.call(
                            callback.scope ? callback.scope : window, 
                            me,
                            o
                        );
                    }
                }
            },
            failure: function(o){
                if (callback.failure) {
                    callback.failure.call(
                        callback.scope ? callback.scope : window, 
                        me,
                        o
                    );
                }
            }
        });
    },
    addRepositoryEntry: function(entry){
        this.entries[entry.key] = entry;
        this.keywords.addEntryKeywords(entry, entry.localizedName);        
        this.keywords.addEntry(entry);
    },
    parseRepositoryDocFileNode: function(node, parent) {
        var entry = {parent: parent}, i, 
            attr, attrName, attrValue,
            name,
            attrs = node.attributes, 
            numAttrs = attrs.length
        ;
        for (i=0; i<numAttrs; i++) {
            attr = attrs.item(i);
            attrName = attr.name;
            attrValue = attr.value;
            switch (attrName) {
                case "lastModifiedDate":
                    attrValue = new Date(parseInt(attrValue, 10));
                    break;
                case "visible":
                    //don't include items that aren't visible
                    //solves http://code.google.com/p/pendular/issues/detail?id=1                    
                    if (attrValue!=="true") {
                        return;
                    }
                case "isDirectory":
                    switch(attrValue) {
                        case "true":
                            attrValue = true;
                            break;
                        default:
                            attrValue = false;
                    }
                    break;
                //case "description":
                case "name":
                    entry.key = (parent ? parent.key : "") + "/" + attrValue;
                    entry.extension = attrValue.substr(attrValue.lastIndexOf(".")+1);
                    break;
                case "localized-name":
                    attrName = "localizedName";
                default:
            }
            entry[attrName] = attrValue;
        }
        if (parent) {
            if (!parent.children) {
                parent.children = {}; 
            }
            parent.children[entry.key] = entry;
        }
        this.addRepositoryEntry(entry);
        return entry;
    },
    parseRepositoryDocNode: function(node, parent){
        var entry;
        switch (node.tagName) {
            case "file":
                entry = this.parseRepositoryDocFileNode(node, parent);
                break;
            case "repository":
                entry = {
                    key: "",
                    isDirectory: true
                };
                break;
            default:
        }
        var childNodes = node.childNodes, 
            numChildNodes = childNodes.length;
        if (numChildNodes) {
            if (entry) {
                entry.childNodes = {};
            }
            for (var i=0; i<numChildNodes; i++) {
                this.parseRepositoryDocNode(childNodes[i], entry);
            }
        }
    },
    parseRepositoryDoc: function(repositoryDoc) {
        this.entries = {};
        this.keywords = new YAHOO.pendular.KeywordDictionary();
        this.parseRepositoryDocNode(repositoryDoc.documentElement, null);
    },
    load: function(callback){
        var me = this, url = this.url + "?component=getSolutionRepositoryDoc";
        YAHOO.util.Connect.asyncRequest("GET", url, {
            success: function(o){
                me.parseRepositoryDoc(o.responseXML);
                if (callback.success) {
                    callback.success.call(
                        callback.scope ? callback.scope : window, 
                        me,
                        o
                    );
                }
            },
            failure: function(o){
                if (callback.failure) {
                    callback.failure.call(
                        callback.scope ? callback.scope : window, 
                        me,
                        o
                    );
                }
            }
        });
    },
    eachEntry: function(callback){
        var key, entries = this.entries;
        for (key in entries) {
            callback(key, entries[key])
        }
    }
});

})();
